<?php
require_once("model/data.php");
require_once("model/image.php");
require_once("model/imageDAO.php");

	/**
	 * Classe du controleur uploadFile
	 */
	class UploadFile {
		
		function __construct() {
			$this->imageDAO = new ImageDAO();
		}
		
		/**
		 *permet de mettre l'image courante sur la premiere image
		 */
		function index(){
			global $imgId,$size,$data,$nbImg;
			$data = new data();
			

			$imgId = 1;
			$size = 480;
			$nbImg = 1;
			
			if(isset($_GET['success'])){
				$data->success= $_GET['success'];
				switch ( $data->success ) {
					case 1:
						$data->msgErreur="Une image de même nom existe déjà.";
						break;
					case 2:
						$data->msgErreur="Le fichier est corrompu.";
						break;
					case 3:
						$data->msgErreur="Le fichier n'est pas au bon Format ou trop gros.";
						break;		
					default:
						break;
					}
				}
			$data->content = "viewUploadFile.php";
			$data->categories = $this->getImageDAO()->getCategories();
			require_once("view/mainView.php");
		}
		
		function uploadImg() {
			$allowedExts = array("jpg", "jpeg", "gif", "png", "bmp");
			$extension = end(explode(".", $_FILES["file"]["name"]));
			if ((($_FILES["file"]["type"] == "image/gif")
					|| ($_FILES["file"]["type"] == "image/jpeg")
					|| ($_FILES["file"]["type"] == "image/png")
					|| ($_FILES["file"]["type"] == "image/pjpeg")
					|| ($_FILES["file"]["type"] == "image/bmp"))
					&& ($_FILES["file"]["size"] < 2000000)
					&& in_array($extension, $allowedExts))
			{
				if ($_FILES["file"]["error"] > 0)
				{
					$success=2;					
					$location = "Location: index.php?controller=uploadFile&action=index&success=$success";
					header($location);
				}
				else
				{
					$urlPath = $this->getImageDAO()->getPath()."/jons/upload/";
					if (file_exists($urlPath.$_FILES["file"]["name"]))
					{
						$success=1;
						$location = "Location: index.php?controller=uploadFile&action=index&success=$success";
						header($location);
					}
					else
					{
						if (!is_dir($urlPath))
							mkdir($urlPath,0777);
						move_uploaded_file($_FILES["file"]["tmp_name"],
								$urlPath.$_FILES["file"]["name"]);
						$img = new Image();
						$img->setPath("jons/upload/" . $_FILES["file"]["name"]);
						if (!isset($_POST["category2"]) || $_POST["category2"]=="" || $_POST["category2"]==NULL){
							$img->setCategory(addslashes($_POST["category"]));
						}else{
							$img->setCategory(addslashes($_POST["category2"]));
						}
						$img->setComment(addslashes($_POST["comment"]));
						$imgId = $this->getImageDAO()->createImg($img);
						$location = "Location: index.php?controller=photo&action=next&imgId=$imgId&size=&categorie="; 
						header($location);
					}
				}
			}
			else
			{
				$success=3;
				$location = "Location: index.php?controller=uploadFile&action=index&success=$success";
				header($location);
			}
		}
		
		/**
		 * return ImageDAO
		 */
		function getImageDAO(){
			return $this->imageDAO;
		}
	}
?> 